﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibHuman;

namespace Human
{
    /// <summary>
    /// Summary description for frmBtdtt.
    /// </summary>
    public class frmBtdpxa : System.Windows.Forms.Form
    {
        private System.Windows.Forms.DataGrid dataGrid1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button butThem;
        private System.Windows.Forms.Button butSua;
        private System.Windows.Forms.Button butLuu;
        private System.Windows.Forms.Button butBoqua;
        private System.Windows.Forms.Button butHuy;
        private System.Windows.Forms.Button butKetthuc;
        private string user;
        private int i_userid, itable;
        private AccessData m;
        Language lan = new Language();
        string s_mmyy = "";
        private DataSet ds = new DataSet();
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.ComboBox matt;
        private System.Windows.Forms.ComboBox cbmaqu;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.MaskedTextBox ma;
        private System.Windows.Forms.MaskedTextBox ten;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public frmBtdpxa(AccessData acc, int userid)
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            lan.Read_Language_to_Xml(this.Name.ToString(), this);
            lan.Changelanguage_to_English(this.Name.ToString(), this);
            m = acc; i_userid = userid;
            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmBtdpxa));
            this.dataGrid1 = new System.Windows.Forms.DataGrid();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.butThem = new System.Windows.Forms.Button();
            this.butSua = new System.Windows.Forms.Button();
            this.butLuu = new System.Windows.Forms.Button();
            this.butBoqua = new System.Windows.Forms.Button();
            this.butHuy = new System.Windows.Forms.Button();
            this.butKetthuc = new System.Windows.Forms.Button();
            this.label3 = new System.Windows.Forms.Label();
            this.matt = new System.Windows.Forms.ComboBox();
            this.cbmaqu = new System.Windows.Forms.ComboBox();
            this.label5 = new System.Windows.Forms.Label();
            this.ma = new System.Windows.Forms.MaskedTextBox();
            this.ten = new System.Windows.Forms.MaskedTextBox();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGrid1
            // 
            this.dataGrid1.AlternatingBackColor = System.Drawing.Color.Lavender;
            this.dataGrid1.BackColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.BackgroundColor = System.Drawing.SystemColors.Control;
            this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors.Control;
            this.dataGrid1.CaptionFont = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dataGrid1.CaptionForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.CaptionVisible = false;
            this.dataGrid1.DataMember = "";
            this.dataGrid1.FlatMode = true;
            this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dataGrid1.ForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.GridLineColor = System.Drawing.Color.Gainsboro;
            this.dataGrid1.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None;
            this.dataGrid1.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
            this.dataGrid1.HeaderForeColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.LinkColor = System.Drawing.Color.Teal;
            this.dataGrid1.Location = new System.Drawing.Point(6, 30);
            this.dataGrid1.Name = "dataGrid1";
            this.dataGrid1.ParentRowsBackColor = System.Drawing.Color.Gainsboro;
            this.dataGrid1.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.ReadOnly = true;
            this.dataGrid1.RowHeaderWidth = 10;
            this.dataGrid1.SelectionBackColor = System.Drawing.Color.CadetBlue;
            this.dataGrid1.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.Size = new System.Drawing.Size(452, 276);
            this.dataGrid1.TabIndex = 4;
            this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged);
            this.dataGrid1.Navigate += new System.Windows.Forms.NavigateEventHandler(this.dataGrid1_Navigate);
            // 
            // label1
            // 
            this.label1.ForeColor = System.Drawing.Color.DarkBlue;
            this.label1.Location = new System.Drawing.Point(8, 311);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(40, 23);
            this.label1.TabIndex = 5;
            this.label1.Text = "Mã :";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label2
            // 
            this.label2.ForeColor = System.Drawing.Color.DarkBlue;
            this.label2.Location = new System.Drawing.Point(81, 312);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(56, 23);
            this.label2.TabIndex = 7;
            this.label2.Text = "Tên :";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // butThem
            // 
            this.butThem.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butThem.ForeColor = System.Drawing.Color.DarkBlue;
            this.butThem.Image = global::Human.Properties.Resources.add;
            this.butThem.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butThem.Location = new System.Drawing.Point(20, 342);
            this.butThem.Name = "butThem";
            this.butThem.Size = new System.Drawing.Size(65, 28);
            this.butThem.TabIndex = 13;
            this.butThem.Text = "  &Thêm";
            this.butThem.Click += new System.EventHandler(this.butThem_Click);
            // 
            // butSua
            // 
            this.butSua.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butSua.ForeColor = System.Drawing.Color.DarkBlue;
            this.butSua.Image = global::Human.Properties.Resources.quick_edit;
            this.butSua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butSua.Location = new System.Drawing.Point(91, 344);
            this.butSua.Name = "butSua";
            this.butSua.Size = new System.Drawing.Size(65, 28);
            this.butSua.TabIndex = 14;
            this.butSua.Text = "  &Sửa";
            this.butSua.Click += new System.EventHandler(this.butSua_Click);
            // 
            // butLuu
            // 
            this.butLuu.Enabled = false;
            this.butLuu.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butLuu.ForeColor = System.Drawing.Color.DarkBlue;
            this.butLuu.Image = global::Human.Properties.Resources.save;
            this.butLuu.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butLuu.Location = new System.Drawing.Point(162, 344);
            this.butLuu.Name = "butLuu";
            this.butLuu.Size = new System.Drawing.Size(65, 28);
            this.butLuu.TabIndex = 11;
            this.butLuu.Text = "   &Lưu";
            this.butLuu.Click += new System.EventHandler(this.butLuu_Click);
            // 
            // butBoqua
            // 
            this.butBoqua.Enabled = false;
            this.butBoqua.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butBoqua.ForeColor = System.Drawing.Color.DarkBlue;
            this.butBoqua.Image = global::Human.Properties.Resources.t_boqua1;
            this.butBoqua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butBoqua.Location = new System.Drawing.Point(234, 344);
            this.butBoqua.Name = "butBoqua";
            this.butBoqua.Size = new System.Drawing.Size(70, 28);
            this.butBoqua.TabIndex = 12;
            this.butBoqua.Text = "&Bỏ qua";
            this.butBoqua.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butBoqua.Click += new System.EventHandler(this.butBoqua_Click);
            // 
            // butHuy
            // 
            this.butHuy.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butHuy.ForeColor = System.Drawing.Color.DarkBlue;
            this.butHuy.Image = global::Human.Properties.Resources.Cancel;
            this.butHuy.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butHuy.Location = new System.Drawing.Point(311, 344);
            this.butHuy.Name = "butHuy";
            this.butHuy.Size = new System.Drawing.Size(65, 28);
            this.butHuy.TabIndex = 15;
            this.butHuy.Text = "   &Hủy";
            this.butHuy.Click += new System.EventHandler(this.butHuy_Click);
            // 
            // butKetthuc
            // 
            this.butKetthuc.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butKetthuc.ForeColor = System.Drawing.Color.DarkBlue;
            this.butKetthuc.Image = global::Human.Properties.Resources.close_r;
            this.butKetthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butKetthuc.Location = new System.Drawing.Point(382, 344);
            this.butKetthuc.Name = "butKetthuc";
            this.butKetthuc.Size = new System.Drawing.Size(65, 28);
            this.butKetthuc.TabIndex = 16;
            this.butKetthuc.Text = " &Thoát";
            this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
            // 
            // label3
            // 
            this.label3.ForeColor = System.Drawing.Color.DarkBlue;
            this.label3.Location = new System.Drawing.Point(-8, 4);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(100, 23);
            this.label3.TabIndex = 0;
            this.label3.Text = "Tỉnh/thành phố :";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // matt
            // 
            this.matt.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.matt.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.matt.Location = new System.Drawing.Point(90, 4);
            this.matt.Name = "matt";
            this.matt.Size = new System.Drawing.Size(116, 21);
            this.matt.TabIndex = 1;
            this.matt.SelectedIndexChanged += new System.EventHandler(this.matt_SelectedIndexChanged);
            this.matt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.matt_KeyDown);
            // 
            // cbmaqu
            // 
            this.cbmaqu.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbmaqu.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.cbmaqu.Location = new System.Drawing.Point(282, 4);
            this.cbmaqu.Name = "cbmaqu";
            this.cbmaqu.Size = new System.Drawing.Size(176, 21);
            this.cbmaqu.TabIndex = 3;
            this.cbmaqu.SelectedIndexChanged += new System.EventHandler(this.maqu_SelectedIndexChanged);
            this.cbmaqu.KeyDown += new System.Windows.Forms.KeyEventHandler(this.maqu_KeyDown);
            // 
            // label5
            // 
            this.label5.ForeColor = System.Drawing.Color.DarkBlue;
            this.label5.Location = new System.Drawing.Point(209, 4);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(73, 23);
            this.label5.TabIndex = 2;
            this.label5.Text = "Quận/huyện :";
            this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // ma
            // 
            this.ma.BackColor = System.Drawing.SystemColors.Control;
            this.ma.Location = new System.Drawing.Point(50, 313);
            this.ma.Name = "ma";
            this.ma.Size = new System.Drawing.Size(50, 20);
            this.ma.TabIndex = 17;
            this.ma.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ma_KeyDown);
            this.ma.Validated += new System.EventHandler(this.ma_Validated);
            this.ma.TypeValidationCompleted += new System.Windows.Forms.TypeValidationEventHandler(this.ma_TypeValidationCompleted);
            // 
            // ten
            // 
            this.ten.BackColor = System.Drawing.SystemColors.Control;
            this.ten.Location = new System.Drawing.Point(142, 313);
            this.ten.Name = "ten";
            this.ten.Size = new System.Drawing.Size(305, 20);
            this.ten.TabIndex = 18;
            this.ten.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ten_KeyDown);
            this.ten.Validated += new System.EventHandler(this.ten_Validated);
            // 
            // frmBtdpxa
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(471, 389);
            this.Controls.Add(this.ten);
            this.Controls.Add(this.ma);
            this.Controls.Add(this.matt);
            this.Controls.Add(this.cbmaqu);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.butKetthuc);
            this.Controls.Add(this.butHuy);
            this.Controls.Add(this.butBoqua);
            this.Controls.Add(this.butLuu);
            this.Controls.Add(this.butSua);
            this.Controls.Add(this.butThem);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.dataGrid1);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "frmBtdpxa";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Danh mục phường xã";
            this.Load += new System.EventHandler(this.frmBtdpxa_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion

        private void frmBtdpxa_Load(object sender, System.EventArgs e)
        {
            user = m.user;
            s_mmyy = DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.Year.ToString().Substring(2, 2);
            matt.DisplayMember = "tentt";
            matt.ValueMember = "matt";
            matt.DataSource = m.get_data("select * from " + user + ".btdtt order by matt").Tables[0];
            try
            {
                matt.SelectedValue = m.Mabv.Substring(0, 3);
            }
            catch { }
            cbmaqu.DisplayMember = "tenquan";
            cbmaqu.ValueMember = "maqu";
            load_quan();
            //load_grid();
            AddGridTableStyle();

        }
        private void load_quan()
        {
            cbmaqu.DataSource = m.get_data("select * from " + user + ".btdquan where matt='" + matt.SelectedValue.ToString() + "'").Tables[0];
            cbmaqu.SelectedValue = m.Maqu();
        }

        private void AddGridTableStyle()
        {
            DataGridTableStyle ts = new DataGridTableStyle();
            ts.MappingName = ds.Tables[0].TableName;
            ts.AlternatingBackColor = Color.Beige;
            ts.BackColor = Color.GhostWhite;
            ts.ForeColor = Color.MidnightBlue;
            ts.GridLineColor = Color.RoyalBlue;
            ts.HeaderBackColor = Color.MidnightBlue;
            ts.HeaderForeColor = Color.Lavender;
            ts.SelectionBackColor = Color.Teal;
            ts.SelectionForeColor = Color.PaleGreen;
            ts.ReadOnly = false;
            ts.RowHeaderWidth = 10;

            DataGridTextBoxColumn TextCol = new DataGridTextBoxColumn();
            TextCol.MappingName = "maphuongxa";
            TextCol.HeaderText = lan.Change_language_MessageText("Mã");
            TextCol.Width = 60;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridTextBoxColumn();
            TextCol.MappingName = "tenpxa";
            TextCol.HeaderText = lan.Change_language_MessageText("Tên");
            TextCol.Width = 320;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);


        }

        private void load_grid()
        {
            if (cbmaqu.SelectedIndex != -1)
            {
                ds = m.get_data("select * from " + user + ".btdpxa where maqu='" + cbmaqu.SelectedValue.ToString() + "'" + " order by maphuongxa");
                dataGrid1.DataSource = ds.Tables[0];

            }
        }

        private void ena_object(bool ena)
        {
            ma.Enabled = ena;
            ten.Enabled = ena;

            butThem.Enabled = !ena;
            butSua.Enabled = !ena;
            butLuu.Enabled = ena;
            butBoqua.Enabled = ena;
            butHuy.Enabled = !ena;
            butKetthuc.Enabled = !ena;
        }

        private void butThem_Click(object sender, System.EventArgs e)
        {
            ma.Text = "";
            ten.Text = "";
            ena_object(true);
            ma.Focus();
        }

        private void butSua_Click(object sender, System.EventArgs e)
        {
            ena_object(true);
            ma.Enabled = false;
            ten.Focus();
        }

        private void butLuu_Click(object sender, System.EventArgs e)
        {
            if (cbmaqu.SelectedIndex == -1)
            {
                MessageBox.Show(lan.Change_language_MessageText("Yêu cầu chọn ma quận huyện!"), AccessData.Msg);
                cbmaqu.Focus();
                return;
            }
            if (ma.Text == "")
            {
                MessageBox.Show(lan.Change_language_MessageText("Yêu cầu nhập mã!"), AccessData.Msg);
                ma.Focus();
                return;
            }
            if (ten.Text == "")
            {
                MessageBox.Show(lan.Change_language_MessageText("Yêu cầu nhập tên!"), AccessData.Msg);
                ten.Focus();
                return;
            }
            m.upd_btdpxa(cbmaqu.SelectedValue.ToString(), cbmaqu.SelectedValue.ToString() + ma.Text, ten.Text);
            load_grid();
            ena_object(false);
        }

        private void butBoqua_Click(object sender, System.EventArgs e)
        {
            ena_object(false);
        }

        private void butHuy_Click(object sender, System.EventArgs e)
        {
            try
            {
                if (m.get_data("select * from " + user + ".btdbn where maphuongxa='" + mapxa + "'").Tables[0].Rows.Count != 0)
                {
                    MessageBox.Show(lan.Change_language_MessageText("Mã phường xã đã sử dụng không thể huỷ!"), AccessData.Msg);
                    return;
                }
                if (MessageBox.Show(lan.Change_language_MessageText("Đồng ý hủy?"), AccessData.Msg, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {

                    m.Execute_data("delete from " + user + ".btdpxa where maphuongxa='" + mapxa + "'");
                    load_grid();
                }
            }
            catch { }

        }

        private void butKetthuc_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }
        string mapxa = "";
        private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
        {
            try
            {
                int i = dataGrid1.CurrentCell.RowNumber;
                ma.Text = dataGrid1[i, 0].ToString().Substring(5, 2);
                ten.Text = dataGrid1[i, 1].ToString();
                mapxa = dataGrid1[i, 0].ToString();
                //load_quan();
                //viettat.Text=dataGrid1[i,2].ToString();
            }
            catch { }
        }

        private void ma_Validated(object sender, System.EventArgs e)
        {
            if (cbmaqu.SelectedIndex == -1)
            {
                MessageBox.Show(lan.Change_language_MessageText("Chưa chọn mã quận!"), AccessData.Msg);
                cbmaqu.Focus();
                return;
            }
            if (ma.Text != "")
            {
                DataRow r = m.getrowbyid(ds.Tables[0], "maphuongxa='" + cbmaqu.SelectedValue.ToString() + ma.Text + "'");
                if (r != null)
                {
                    MessageBox.Show(lan.Change_language_MessageText("Mã này đã có!"), AccessData.Msg);
                    ma.Focus();
                    return;
                }
            }
        }

        private void ten_Validated(object sender, System.EventArgs e)
        {
            if (ten.Text != "") ten.Text = m.Caps(ten.Text);
        }

        private void matt_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            try
            {
                load_quan();
            }
            catch { }
        }

        private void matt_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) SendKeys.Send("{Tab}{F4}");
        }

        private void maqu_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            try
            {
                load_grid();
            }
            catch { }
        }

        private void maqu_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }

        private void viettat_Validated(object sender, System.EventArgs e)
        {

        }

        private void ma_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) SendKeys.Send("{Tab}");
        }

        private void ten_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) SendKeys.Send("{Tab}");
        }

        private void dataGrid1_Navigate(object sender, NavigateEventArgs ne)
        {

        }

        private void ma_TypeValidationCompleted(object sender, TypeValidationEventArgs e)
        {

        }
    }
}
